package com.clive.dao;

import com.clive.bean.DishesCategory;
import com.clive.util.DbcpUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DishesCategoryDaoImpl implements DishesCategoryDao {

    @Override
    public int addDishesCategory(DishesCategory dishesCategory) {
        Connection conn = DbcpUtils.getconnection();
        PreparedStatement pst = null;
        String sql = "INSERT INTO dishes_category(dishes_category_name,status,created,updated) VALUE(?,?,?,?);";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, dishesCategory.getDishesCategoryName());
            pst.setInt(2, dishesCategory.getStatus());
            pst.setDate(3, new Date(dishesCategory.getCreated().getTime()));
            pst.setDate(4, new Date(dishesCategory.getUpdated().getTime()));
            int i = pst.executeUpdate();
            return i;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            DbcpUtils.closeAll(conn, pst);
        }
        return 0;
    }

    @Override
    public int deleteDishesCategory(Integer dishesCategoryId) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int updateDishesCategory(DishesCategory dishesCategory) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public List<DishesCategory> findDishesCategoryAll() {
        Connection conn = DbcpUtils.getconnection();
        PreparedStatement pst = null;
        ResultSet result = null;
        String sql = "SELECT * FROM dishes_category WHERE status = 1";
        try {
            pst = conn.prepareStatement(sql);
            result = pst.executeQuery();
            List<DishesCategory> list = new ArrayList<DishesCategory>();
            while (result.next()) {
                DishesCategory category = new DishesCategory();
                category.setId(result.getInt("id"));
                category.setDishesCategoryName(result.getString("dishes_category_name"));
                category.setStatus(result.getInt("status"));
                category.setCreated(result.getDate("created"));
                category.setUpdated(result.getDate("updated"));
                list.add(category);
            }
            return list;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DbcpUtils.closeAll(conn, pst, result);
        }
        return null;

    }

}
